<?php
namespace lib;

class Dysms
{
    // Access Key ID
    private $accessKeyId = 'LTAI4G9Bp5rKBximuJFS9p11';
    // Access Key Secret
    private $accessKeySecret = 'XR59ljPZDH2t9mIUjOlmgnmNRkmUYi';
    // 短信签名
    private $signName = '星云盾';
    // 模版ID
    private $templateCode = 'SMS_204490075';
    // 服务器地址
    private $host = 'https://dysmsapi.aliyuncs.com/';
    /**
     * 构造
     */
    public function __construct($config = array())
    {
        //$config = array(
        //    'access_key' => '***********************',
        //    'access_secret' => '***********************',
        //    'sign_name' => '短信签名',
        //    'template_code' => 'SMS_111111111',
        //);
//        $this->accessKeyId = isset($config['access_key']) ? $config['access_key'] : '';
//        $this->accessKeySecret = isset($config['access_secret']) ? $config['access_secret'] : '';
//        $this->signName = isset($config['sign_name']) ? $config['sign_name'] : '';
//        $this->templateCode = isset($config['template_code']) ? $config['template_code'] : '';
    }
    /**
     * 发送验证码
     */
    public function send($mobile, $code)
    {
        $params = array(
            'SignName' => $this->signName,
            'Format' => 'JSON',
            'Version' => '2017-05-25',
            'AccessKeyId' => $this->accessKeyId,
            'SignatureVersion' => '1.0',
            'SignatureMethod' => 'HMAC-SHA1',
            'SignatureNonce' => uniqid(),
            'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'),
            'Action' => 'SendSms',
            'TemplateCode' => $this->templateCode,
            'PhoneNumbers' => $mobile,
            'TemplateParam' => '{"code":"' . $code . '"}',
        );
        $params['Signature'] = $this->make($params);
        $url = $this->host . '?' . http_build_query($params);
        $result = json_decode($this->get($url), true);
        if (!isset($result['Code'])) {
            return array(
                'errcode' => 3001,
                'errmsg' => '远程服务器无应答',
            );
        }
        if ('OK' !== $result['Code']) {
            return array(
                'errcode' => $result['Code'],
                'errmsg' => $result['Message'],
            );
        }
        return array(
            'errcode' => 0,
            'errmsg' => 'success',
        );
    }
    /**
     * 解码
     */
    private function denc($str)
    {
        $str = urlencode($str);
        $str = preg_replace('/\+/', '%20', $str);
        $str = preg_replace('/\*/', '%2A', $str);
        $str = preg_replace('/%7E/', '~', $str);
        return $str;
    }
    /**
     * 签名
     */
    private function make($params)
    {
        ksort($params);
        $query = '';
        foreach ($params as $key => $value) {
            $query .= '&' . $this->denc($key) . '=' . $this->denc($value);
        }
        $string = 'GET&%2F&' . $this->denc(substr($query, 1));
        $signature = base64_encode(hash_hmac('sha1', $string, $this->accessKeySecret . '&', true));
        return $signature;
    }
    /**
     * http发送get请求
     */
    private function get($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $out = curl_exec($ch);
        curl_close($ch);
        return $out;
    }
}